package com.jdew.thkj.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jdew.thkj.R;
import com.jdew.thkj.bean.HuiFenBean;
import com.jdew.thkj.onitemInterface.OnCopyClickListener;
import com.jdew.thkj.onitemInterface.OnItemClickListener;
import com.jdew.thkj.onitemInterface.OnItemPictureClickListener;
import com.jdew.thkj.onitemInterface.OnItemPictureClickSelectListener;
import com.jdew.thkj.onitemInterface.OnShareClickListener;
import com.jdew.thkj.utils.RegexValidateUtils;
import com.jdew.thkj.view.FriendsCircleImageLayout;
import com.jdew.thkj.view.NineGridTestLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yangyang on 2017/9/18.
 */

public class HuifenAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private List<HuiFenBean.DataBean> dataList;
    private LayoutInflater layoutInflater;
    private Context context;
    private View itemView;
    private OnItemClickListener mItemClickListener;
    private OnItemPictureClickSelectListener onItemPictureClickListener;
    private OnShareClickListener onShareClickListener;
    private OnCopyClickListener onCopyClickListener;
    private int state;
    public List<String> UriList = new ArrayList<>();

    @Override
    public void onClick(View v) {
        if (mItemClickListener != null) {
            mItemClickListener.onItemClick((Integer) v.getTag());
        }
    }

    public void setOnShareClickListener(OnShareClickListener listener) {
        this.onShareClickListener = listener;
    }

    public void setOncopyClickListener(OnCopyClickListener listener) {
        this.onCopyClickListener = listener;
    }

    public void setOnItemPictureClickListener(OnItemPictureClickSelectListener listener) {
        this.onItemPictureClickListener = listener;
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    public HuifenAdapter(Context context, List<HuiFenBean.DataBean> dataList, int state) {
        this.context = context;
        this.dataList = dataList;
        this.state = state;
        layoutInflater = LayoutInflater.from(context);
    }


    public void setDataList(List<HuiFenBean.DataBean> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        itemView = layoutInflater.inflate(R.layout.huifen_item, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final MyViewHolder myviewHolder = (MyViewHolder) holder;
        myviewHolder.BindView(holder, position);
        myviewHolder.itemView.setTag(position);
        myviewHolder.detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mItemClickListener) {
                    mItemClickListener.onItemClick(position);
                }
            }
        });
        myviewHolder.friendsCircleImageLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onItemPictureClickListener) {
                    UriList.clear();
                    for (int i = 0; i < dataList.get(position).getPictureList().size(); i++) {
                        UriList.add(dataList.get(position).getPictureList().get(i).getPictureUrl());
                    }
                    myviewHolder.friendsCircleImageLayout.setUrlList(UriList);
                }
            }
        });
        myviewHolder.share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onShareClickListener) {
                    onShareClickListener.onItemClick(position);
                }
            }
        });
        myviewHolder.copy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onCopyClickListener) {
                    onCopyClickListener.onItemClick(position);
                }
            }
        });
        myviewHolder.friendsCircleImageLayout.setListener(onItemPictureClickListener);
        myviewHolder.friendsCircleImageLayout.setItemPosition(position);
        myviewHolder.friendsCircleImageLayout.setSpacing(5); //动态设置图片之间的间隔
        myviewHolder.friendsCircleImageLayout.setUrlList(UriList);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

     class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.i_avatar)
        ImageView iAvatar;
        @Bind(R.id.t_nickname)
        TextView tNickname;
        @Bind(R.id.t_time)
        TextView tTime;
        @Bind(R.id.sharenum)
        TextView shareNum;
        @Bind(R.id.t_content)
        TextView tContent;
        @Bind(R.id.detail)
        TextView detail;
        @Bind(R.id.friendsCircleImageLayout)
        NineGridTestLayout friendsCircleImageLayout;
        @Bind(R.id.contents)
        RelativeLayout contents;
        @Bind(R.id.commets_area)
        TextView commetsArea;
        @Bind(R.id.share)
        TextView share;
        @Bind(R.id.copy)
        TextView copy;

        MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void BindView(RecyclerView.ViewHolder holder, final int position) {
            UriList.clear();
            for (int i = 0; i < dataList.get(position).getPictureList().size(); i++) {
                UriList.add(dataList.get(position).getPictureList().get(i).getPictureUrl());
            }
            tNickname.setText(dataList.get(position).getFounderName());
            tTime.setText(dataList.get(position).getFounderTime());
            tContent.setText(RegexValidateUtils.unicodeToString(dataList.get(position).getContentText()).replace("\\n", "\n").replace("\\", "").replace("￥","").replace("$","").replace("(","").replace(")",""));
            shareNum.setText(dataList.get(position).getShareCount());
            if (state == 2) {
                detail.setText("查看详情");
                commetsArea.setText("【正确下单步骤：复制这条评论信息→点开手机淘宝领券下单！！】");
            } else if (state == 1 ||state == 3) {
                copy.setText("复制评论");
                share.setText("分享图片");
                detail.setVisibility(View.GONE);
                commetsArea.setText(RegexValidateUtils.unicodeToString(dataList.get(position).getCopyComment()).replace("\\n", "\n").replace("\\", ""));
            }
            RequestOptions options = new RequestOptions().override(310, 310).placeholder(R.mipmap.ploder).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true);
            Glide.with(context).load(dataList.get(position).getAdminImgUrl()).apply(options).into(iAvatar);
        }
    }
}
